1 /*
2 * @(#)ExtensionFileFilter.java 1.9 99/04/23
3 *
4 * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved.
5 *
6 * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
7 * modify and redistribute this software in source and binary code form,
8 * provided that i) this copyright notice and license appear on all copies of
9 * the software; and ii) Licensee does not utilize the software in a manner
10 * which is disparaging to Sun.
11 *
12 * This software is provided "AS IS," without a warranty of any kind. ALL
13 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
14 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
15 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
16 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
17 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
18 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
19 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
20 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
21 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
22 * POSSIBILITY OF SUCH DAMAGES.
23 *
24 * This software is not designed or intended for use in on-line control of
25 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
26 * the design, construction, operation or maintenance of any nuclear
27 * facility. Licensee represents and warrants that it will not use or
28 * redistribute the Software for such purposes.
29 */
30 /***
31 *
32 */
33
34 package nmp.file_utils;
35
36 import java.io.File;
37 import java.util.Hashtable;
38 import java.util.Enumeration;
39 import javax.swing.*;
40 import javax.swing.filechooser.*;
41
42 /***
43 * A convenience implementation of FileFilter that filters out
44 * all files except for those type extensions that it knows about.
45 *
46 * Extensions are of the type ".foo", which is typically found on
47 * Windows and Unix boxes, but not on Macinthosh. Case is ignored.
48 *
49 * Example - create a new filter that filerts out all files
50 * but gif and jpg image files:
51 *
52 * JFileChooser chooser = new JFileChooser();
53 * ExtensionFileFilter filter = new ExtensionFileFilter(
54 * new String{"gif", "jpg"}, "JPEG & GIF Images")
55 * chooser.addChoosableFileFilter(filter);
56 * chooser.showOpenDialog(this);
57 *
58 * @version 1.9 04/23/99
59 * @author Jeff Dinkins
60 */
61 public class ExtensionFileFilter extends FileFilter {
62
63 private static String TYPE_UNKNOWN = "Type Unknown";
64 private static String HIDDEN_FILE = "Hidden File";
65
66 private Hashtable filters = null;
67 private String description = null;
68 private String fullDescription = null;
69 private boolean useExtensionsInDescription = true;
70
71 /***
72 * Creates a file filter. If no filters are added, then all
73 * files are accepted.
74 *
75 * @see #addExtension
76 */
77 public ExtensionFileFilter() {
78 this.filters = new Hashtable();
79 }
80
81 /***
82 * Creates a file filter that accepts files with the given extension.
83 * Example: new ExtensionFileFilter("jpg");
84 *
85 * @see #addExtension
86 */
87 public ExtensionFileFilter(String extension) {
88 this(extension,null);
89 }
90
91 /***
92 * Creates a file filter that accepts the given file type.
93 * Example: new ExtensionFileFilter("jpg", "JPEG Image Images");
94 *
95 * Note that the "." before the extension is not needed. If
96 * provided, it will be ignored.
97 *
98 * @see #addExtension
99 */
100 public ExtensionFileFilter(String extension, String description) {
101 this();
102 if(extension!=null) addExtension(extension);
103 if(description!=null) setDescription(description);
104 }
105
106 /***
107 * Creates a file filter from the given string array.
108 * Example: new ExtensionFileFilter(String {"gif", "jpg"});
109 *
110 * Note that the "." before the extension is not needed adn
111 * will be ignored.
112 *
113 * @see #addExtension
114 */
115 public ExtensionFileFilter(String[] filters) {
116 this(filters, null);
117 }
118
119 /***
120 * Creates a file filter from the given string array and description.
121 * Example: new ExtensionFileFilter(String {"gif", "jpg"}, "Gif and JPG Images");
122 *
123 * Note that the "." before the extension is not needed and will be ignored.
124 *
125 * @see #addExtension
126 */
127 public ExtensionFileFilter(String[] filters, String description) {
128 this();
129 for (int i = 0; i < filters.length; i++) {
130 // add filters one by one
131 addExtension(filters[i]);
132 }
133 if(description!=null) setDescription(description);
134 }
135
136 /***
137 * Return true if this file should be shown in the directory pane,
138 * false if it shouldn't.
139 *
140 * Files that begin with "." are ignored.
141 *
142 * @see #getExtension
143 * @see FileFilter#accepts
144 */
145 public boolean accept(File f) {
146 if(f != null) {
147 if(f.isDirectory()) {
148 return true;
149 }
150 String extension = getExtension(f);
151 if(extension != null && filters.get(getExtension(f)) != null) {
152 return true;
153 };
154 }
155 return false;
156 }
157
158 /***
159 * Return the extension portion of the file's name .
160 *
161 * @see #getExtension
162 * @see FileFilter#accept
163 */
164 public String getExtension(File f) {
165 if(f != null) {
166 String filename = f.getName();
167 int i = filename.lastIndexOf('.');
168 if(i>0 && i<filename.length()-1) {
169 return filename.substring(i+1).toLowerCase();
170 };
171 }
172 return null;
173 }
174
175 /***
176 * Adds a filetype "dot" extension to filter against.
177 *
178 * For example: the following code will create a filter that filters
179 * out all files except those that end in ".jpg" and ".tif":
180 *
181 * ExtensionFileFilter filter = new ExtensionFileFilter();
182 * filter.addExtension("jpg");
183 * filter.addExtension("tif");
184 *
185 * Note that the "." before the extension is not needed and will be ignored.
186 */
187 public void addExtension(String extension) {
188 if(filters == null) {
189 filters = new Hashtable(5);
190 }
191 filters.put(extension.toLowerCase(), this);
192 fullDescription = null;
193 }
194
195
196 /***
197 * Returns the human readable description of this filter. For
198 * example: "JPEG and GIF Image Files (*.jpg, *.gif)"
199 *
200 * @see setDescription
201 * @see setExtensionListInDescription
202 * @see isExtensionListInDescription
203 * @see FileFilter#getDescription
204 */
205 public String getDescription() {
206 if(fullDescription == null) {
207 if(description == null || isExtensionListInDescription()) {
208 fullDescription = description==null ? "(" : description + " (";
209 // build the description from the extension list
210 Enumeration extensions = filters.keys();
211 if(extensions != null) {
212 fullDescription += "." + (String) extensions.nextElement();
213 while (extensions.hasMoreElements()) {
214 fullDescription += ", " + (String) extensions.nextElement();
215 }
216 }
217 fullDescription += ")";
218 } else {
219 fullDescription = description;
220 }
221 }
222 return fullDescription;
223 }
224
225 /***
226 * Sets the human readable description of this filter. For
227 * example: filter.setDescription("Gif and JPG Images");
228 *
229 * @see setDescription
230 * @see setExtensionListInDescription
231 * @see isExtensionListInDescription
232 */
233 public void setDescription(String description) {
234 this.description = description;
235 fullDescription = null;
236 }
237
238 /***
239 * Determines whether the extension list (.jpg, .gif, etc) should
240 * show up in the human readable description.
241 *
242 * Only relevent if a description was provided in the constructor
243 * or using setDescription();
244 *
245 * @see getDescription
246 * @see setDescription
247 * @see isExtensionListInDescription
248 */
249 public void setExtensionListInDescription(boolean b) {
250 useExtensionsInDescription = b;
251 fullDescription = null;
252 }
253
254 /***
255 * Returns whether the extension list (.jpg, .gif, etc) should
256 * show up in the human readable description.
257 *
258 * Only relevent if a description was provided in the constructor
259 * or using setDescription();
260 *
261 * @see getDescription
262 * @see setDescription
263 * @see setExtensionListInDescription
264 */
265 public boolean isExtensionListInDescription() {
266 return useExtensionsInDescription;
267 }
268 }
This page was automatically generated by Maven